/////precompiled////////
#include "precompiled.h"
////////////////////////


#include "prs_GrfImage.h"
#include "prs_Unit.h"
using namespace prs;

// TRuleGrfImage //////////////////////////////////////////////////////////////

//
TRuleGrfImage::TRuleGrfImage( snt::text::TContext &Context
							, svl::TVariant &VImage
							, svl::TVariant &VType
							, svl::TVariant &VParentNode)
: vImage(VImage)
, vType (VType)
, vParentNode(VParentNode)
{
	snt::TFVoid          F;
	snt::TReturn         Return;
	snt::text::TSequence T(Context);
	snt::text::TNumber   N(Context);
	snt::text::TIdent	   I(Context);

	this->Name(GetClassName())
	| T.New("image")	   & T.New("=") & I.NewClosure(this,&TRuleGrfImage::FImage)  & Return.New()
	| T.New("fillImage") & T.New("=") & I.NewClosure(this,&TRuleGrfImage::FFImage) & Return.New()
	;
}

bool TRuleGrfImage::FImage(snt::text::TIdent::TFuncClosureDoEntryParams &Params){
	vType  = "single";
	
	if( InitImage(Params.Ident.GetValue().String()) )
		return true;
	Params.OutError = "Missing image";
	return false;
}

bool TRuleGrfImage::FFImage(snt::text::TIdent::TFuncClosureDoEntryParams &Params){
	vType  = "fill";
	
	if( InitImage(Params.Ident.GetValue().String()) )
		return true;
	 Params.OutError = "Missing image";
	 return false;
}

bool TRuleGrfImage::InitImage(svl::TString &Name){
	TNode *Parent = dynamic_cast<prs::TNode*>(vParentNode.Pointer());
	
	TNode *Node = TNodeSearch(Parent)
	 .SearchNearParentByType(TNodeFrm::nodeType_Unit)
	 .GetFirst();
	TNodeUnit *Unit = dynamic_cast<TNodeUnit*>(Node);
	vImage = Unit->Images.Array.GetImage( Name );
	if ( vImage.Pointer() )
		return true;
	return false;
}



// TRuleGrfImageBorderOld //////////////////////////////////////////////////////////////
//
TRuleGrfImageBorderOld::TRuleGrfImageBorderOld( snt::text::TContext &Context
											, svl::TVariant &VImages
											, svl::TVariant &VParentNode)
: vParentNode(VParentNode)
, vImages(VImages)
, Num(0)
, vThis(this)
{ 
	for( int i=0; i<9; i++ ) Images[i] = NULL;
	
	snt::TFVoid          F;
	snt::TReturn         Return;
	snt::text::TSequence T(Context);
	snt::text::TNumber   N(Context);
	snt::text::TIdent	   I(Context);

	this->Name(GetClassName())
	| T.New("imageBorderOld") & T.New("=") & A1.New()
	;

	A1
	| I.NewClosure(this,&TRuleGrfImageBorderOld::FImage) & A2.New()
	|													  A2.New()
	;
	
	A2
	| T.New(",") & F.New(FAdd,vThis) & A1.New()
	| F.New(FEnd,vThis) & Return.New()
	;
	
}

bool TRuleGrfImageBorderOld::FImage(snt::text::TIdent::TFuncClosureDoEntryParams &Params){
	
	if( Num.Integer() >= 9 ){
		Params.OutError = "More than 9 images.";
		return false;
	}
	
	TNode *Parent = dynamic_cast<prs::TNode*>(vParentNode.Pointer());
	
	TNode *Node = TNodeSearch(Parent)
	 .SearchNearParentByType(TNodeFrm::nodeType_Unit)
	 .GetFirst();
	TNodeUnit *Unit = dynamic_cast<TNodeUnit*>(Node);
	svl::TString aa = Params.Ident.GetValue();
	grf::TImageGdiP *img = Unit->Images.Array.GetImage(Params.Ident.GetValue());
	if ( !img ){
		Params.OutError = "Missing image.";
		return false;
	}
	Images[Num.Integer()] = img;
	
	return true;
}

void TRuleGrfImageBorderOld::FEnd(snt::TParams &Params){
	TRuleGrfImageBorderOld *This = dynamic_cast<TRuleGrfImageBorderOld*>( Params.Pop().Pointer() );
	This->vImages = (int)(This->Images);
}

void TRuleGrfImageBorderOld::FAdd(snt::TParams &Params){
	TRuleGrfImageBorderOld *This = dynamic_cast<TRuleGrfImageBorderOld*>( Params.Pop().Pointer() );
	This->Num = This->Num.Integer() + 1;
}

// TRuleGrfImageBorder //////////////////////////////////////////////////////////////
//
TRuleGrfImageBorder::TRuleGrfImageBorder( snt::text::TContext &Context
										, svl::TVariant &VBorderInfo
										, svl::TVariant &VParentNode)
: vParentNode(VParentNode)
, vBorderInfo(VBorderInfo)
, vThis(this)
{ 
	snt::TFVoid          F;
	snt::TReturn         Return;
	snt::text::TSequence T(Context);
	snt::text::TNumber   N(Context);
	snt::text::TIdent    I(Context);

	this->Name(GetClassName())
	| T.New("imageBorder") & T.New("=") 
	 & I.NewClosure(this,&TRuleGrfImageBorder::FImage) & T.New(",") & A.New()
	;
	A
	| T.New("multi")   & F.New(FSetMulti,vThis)   & Border.New() & Return.New()
	| T.New("nomulti") & F.New(FSetNoMulti,vThis) & Border.New() & Return.New()
	;
	
	Border
	| T.New(",") & N.New(V1) & T.New(",") & N.New(V2) & T.New(",")
	 & N.New(V3) & T.New(",") & N.New(V4) & F.New(FEnd,vThis) & Return.New()
	;
}

bool TRuleGrfImageBorder::FImage(snt::text::TIdent::TFuncClosureDoEntryParams &Params){
	 
	TNode *Parent = dynamic_cast<prs::TNode*>(vParentNode.Pointer());
	
	TNode *Node = TNodeSearch(Parent)
	 .SearchNearParentByType(TNodeFrm::nodeType_Unit)
	 .GetFirst();
	
	TNodeUnit *Unit = dynamic_cast<TNodeUnit*>(Node);
	svl::TString aa = Params.Ident.GetValue();
	grf::TImageGdiP *img = Unit->Images.Array.GetImage(Params.Ident.GetValue());
	
	if ( !img ){
		Params.OutError = "Missing image.";
		return false;
	}
	borderInfo.image = img;
	return true;
}

void TRuleGrfImageBorder::FSetMulti(snt::TParams &Params){
	TRuleGrfImageBorder *This = dynamic_cast<TRuleGrfImageBorder*>( Params.Pop().Pointer() );
	This->borderInfo.multi = true;
}

void TRuleGrfImageBorder::FSetNoMulti(snt::TParams &Params){
	TRuleGrfImageBorder *This = dynamic_cast<TRuleGrfImageBorder*>( Params.Pop().Pointer() );
	This->borderInfo.multi = false;
}

void TRuleGrfImageBorder::FEnd(snt::TParams &Params){
	TRuleGrfImageBorder *This = dynamic_cast<TRuleGrfImageBorder*>( Params.Pop().Pointer() );
	This->borderInfo.t = This->V1.Integer();
	This->borderInfo.r = This->V2.Integer();
	This->borderInfo.b = This->V3.Integer();
	This->borderInfo.l = This->V4.Integer();
	This->vBorderInfo = (int)(&This->borderInfo);
}

// TRuleGrfImageCenter //////////////////////////////////////////////////////////////
//
TRuleGrfImageCenter::TRuleGrfImageCenter( snt::text::TContext &Context
										, svl::TVariant &VImage
										, svl::TVariant &VParentNode)
: vImage(VImage)
, vParentNode(VParentNode)
{
	snt::TFVoid          F;
	snt::TReturn         Return;
	snt::text::TSequence T(Context);
	snt::text::TNumber   N(Context);
	snt::text::TIdent	   I(Context);

	this->Name(GetClassName())
	| T.New("imagecenter") & T.New("=") & I.NewClosure(this,&TRuleGrfImageCenter::FImage) & Return.New()
	;
}

bool TRuleGrfImageCenter::FImage(snt::text::TIdent::TFuncClosureDoEntryParams &Params){
	TNode *Parent = dynamic_cast<prs::TNode*>(vParentNode.Pointer());
	
	TNode *Node = TNodeSearch(Parent)
	 .SearchNearParentByType(TNodeFrm::nodeType_Unit)
	 .GetFirst();
	TNodeUnit *Unit = dynamic_cast<TNodeUnit*>(Node);
	vImage = Unit->Images.Array.GetImage(Params.Ident.GetValue().String());
	
	if ( vImage.Pointer() )
		return true;
	Params.OutError = "Missing image";
	return false;
}
